package com.example.administrator.szb.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BaseActivity;
import com.example.administrator.szb.activity.myyewu.YWGLYESActivity;
import com.example.administrator.szb.bean.DownIngBean;
import com.example.administrator.szb.bean.Login;
import com.example.administrator.szb.bean.ResultBean;
import com.example.administrator.szb.config.FragmentsFlag;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.AppUtils;
import com.example.administrator.szb.util.FinishActivity;
import com.example.administrator.szb.util.GetJsonDataUtil;
import com.example.administrator.szb.util.GlideCacheUtil;
import com.example.administrator.szb.util.JPushUtils;
import com.example.administrator.szb.util.Permission;
import com.example.administrator.szb.util.SPUtils;
import com.example.administrator.szb.util.ScreenExpressionUtils;
import com.example.administrator.szb.util.SharedPreferencesUtil;
import com.example.administrator.szb.util.UpdateAppService;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.im.android.api.JMessageClient;

/**
 * Created by D N on 2017/10/10.
 * 设置
 */

public class SZActivity extends BaseActivity {
    @Bind(R.id.sz_image_fh)
    ImageView szImageFh;
    @Bind(R.id.sz_text_xgmm)
    TextView szTextXgmm;
    @Bind(R.id.sz_ll_qchc)
    LinearLayout szLlQchc;
    @Bind(R.id.sz_text_yjfk)
    TextView szTextYjfk;
    @Bind(R.id.sz_ll_bbgx)
    LinearLayout szLlBbgx;
    @Bind(R.id.sz_text_xy)
    TextView szTextXy;
    @Bind(R.id.sz_text_lxwm)
    TextView szTextLxwm;
    @Bind(R.id.sz_button_tczh)
    Button szButtonTczh;
    @Bind(R.id.sz_text_cache)
    TextView szTextCache;
    @Bind(R.id.textView16)
    TextView textView16;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sz_activity);
        SampleApplicationLike.getActivitiesInstance().add(this);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        textView16.setText(AppUtils.getVersionName(this));
        szTextCache.setText(GlideCacheUtil.getInstance().getCacheSize(this));
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @OnClick({R.id.sz_image_fh, R.id.sz_text_xgmm, R.id.sz_ll_qchc, R.id.sz_text_yjfk, R.id.sz_ll_bbgx, R.id.sz_text_xy, R.id.sz_text_lxwm, R.id.sz_button_tczh})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.sz_image_fh:
                finish();
                break;
            case R.id.sz_text_xgmm:
                startActivity(new Intent(this, XGMMActivity.class));
                break;
            case R.id.sz_ll_qchc:
                showPopupwindow(R.layout.popuwindow_tcdl, ScreenExpressionUtils.dip2px(this, 324), ScreenExpressionUtils.dip2px(this, 164), R.layout.sz_activity, new CallbackPopuwindow() {
                    @Override
                    public void doSomeThing(View view) {
                        ((TextView) view.findViewById(R.id.textView14)).setText("清除缓存");
                        ((TextView) view.findViewById(R.id.textView8)).setText("是否确认清除缓存");
                        ((TextView) view.findViewById(R.id.textView15)).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {

                                GlideCacheUtil.getInstance().clearImageAllCache(SZActivity.this);
                                szTextCache.setText("0M");
                                popWindow.dissmiss();
//                                SharedPreferencesUtil.savaData(SZActivity.this,new Login.DataBean(),SampleApplicationLike.getKey());
//                                SampleApplicationLike.setUserloginInstance(new Login.DataBean());
//                                startActivity(new Intent(SZActivity.this,LoginNewActivity.class));
//                                SampleApplicationLike.exitActivities();
                            }
                        });

                        ((TextView) view.findViewById(R.id.textView9)).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                popWindow.dissmiss();
                            }
                        });
                    }
                });
                break;
            case R.id.sz_text_yjfk:
                Intent intent = new Intent(this, YJFKActivity.class);
                intent.putExtra("who", FragmentsFlag.Activity_SZ.ordinal());
                startActivity(intent);
                break;
            case R.id.sz_ll_bbgx:
                Permission.isHaveRootActivity(this,Permission.SD_ARR,Permission.SD_CODE,this);
                break;
            case R.id.sz_text_xy:
                Intent intent_xy = new Intent(SZActivity.this,GZXYActivity.class);//额，
                intent_xy.putExtra("title","市值宝服务协议");
                intent_xy.putExtra("id",4);
                startActivity(intent_xy);
                break;
            case R.id.sz_text_lxwm:
                break;
            case R.id.sz_button_tczh:
//                SharedPreferencesUtil.savaData(this,new Login.DataBean(),SampleApplicationLike.getKey());
//                SampleApplicationLike.setUserloginInstance(new Login.DataBean());
//                startActivity(new Intent(this,LoginNewActivity.class));
//                SampleApplicationLike.exitActivities();
                showPopupwindow(R.layout.popuwindow_tcdl, ScreenExpressionUtils.dip2px(this, 324), ScreenExpressionUtils.dip2px(this, 164), R.layout.sz_activity, new CallbackPopuwindow() {
                    @Override
                    public void doSomeThing(View view) {
                        ((TextView) view.findViewById(R.id.textView15)).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                popWindow.dissmiss();
                                logouts();

//                                popWindow.dissmiss();
//                                SharedPreferencesUtil.savaData(SZActivity.this, new Login.DataBean(), SampleApplicationLike.getKey());
//                                SampleApplicationLike.setUserloginInstance(new Login.DataBean());
//                                startActivity(new Intent(SZActivity.this, LoginNewActivity.class));
//                                SampleApplicationLike.exitActivities();
                            }
                        });

                        ((TextView) view.findViewById(R.id.textView9)).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                popWindow.dissmiss();
                            }
                        });
                    }
                });
                break;
        }
    }

    private void requestQuitLogin() {
        HashMap<String,String> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("users_id",SampleApplicationLike.getUserloginInstance().getId()+"");
        String url = URLAddress.URLBASE + URLAddress.URL_QUIT;
        String json = GetJsonDataUtil.simpleMapToJsonStr(map);
        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, ResultBean.class, url, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                ResultBean resultBean = (ResultBean) response;
                if (resultBean.getResult() == 1){
                    SharedPreferencesUtil.savaData(SZActivity.this, new Login.DataBean(), SampleApplicationLike.getKey());
                    SampleApplicationLike.setUserloginInstance(new Login.DataBean());
                    //startActivity(new Intent(SZActivity.this, LoginNewActivity_two.class));
                    //new HomeActivity().chooseTab(0);
                   // HomeActivity.isClick = 2;
                    logouts();

                    //SampleApplicationLike.exitActivities();
                }else{
                    logouts();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                logouts();
            }
        });
    }
    private void logouts(){

        JMessageClient.logout();//极光退出登录
        JPushUtils.delete();
        YWGLYESActivity.ywbqglBean = null;
        JGGLYESActivity.gwxqBean = null;
        SPUtils.clear();
        SPUtils.setDatas(SPUtils.IS_LOADED_YDY,1);
        FinishActivity.finish();
        Intent intent = new Intent(this,HomeActivity.class);
        startActivity(intent);
    }
    UpdateAppService updateAppService;
    @Override
    protected void onRequestPermissionsSuccess(int requestCode) {
        switch (requestCode){
            case Permission.SD_CODE:
                textView16.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        updateAppService = new UpdateAppService(SZActivity.this,textView16,true);
                        updateAppService.checkVersion();
                    }
                },500);

                break;
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN,priority = 100) //在ui线程执行 优先级100
    public void onDataSynEvent(DownIngBean event) {
        if (updateAppService!=null){
            updateAppService.showPoping(event);
        }
    }

}
